<?php
/**
 * @author	hequan
 * @todo	全量从文件导入淘宝的商品分类数据信息
 */
require dirname(dirname(dirname(dirname(__FILE__)))) . '/www/index.php';
require ROOT_PATH . '/lib/openapi/top/TopSdk.php';


error_reporting(E_ALL|E_STRICT);
ini_set("display_errors","on");
ini_set('memory_limit', -1);
ini_set('auto_detect_line_endings', 'on');
set_time_limit(0);
setlocale(LC_ALL, 'zh_CN');


function usage($argv, $msg = "")
{
	printf("%s <catdir> 1|3|9\n", $argv[0]);
	if(!empty($msg)){
		printf("%s\n", $msg);
	}
	exit(1);
}

if(count($argv) != 3){
	usage($argv);
}

if(!file_exists($argv[1])){
	usage($argv, "{$argv[1]} 不是正规目录");
}

if(!in_array($argv[2], array(1, 3, 9))){
	usage($argv, "{$argv[2]} 必须是1或者3或者9");
}

if($argv[2] == 1)
{
	$taobaocat_obj = Factory::getData('taobaocat');
	$taobaoitemprop_obj = Factory::getData('taobaoitemprop');
	$taobaoitempropvalue_obj = Factory::getData('taobaoitempropvalue');
}
else
{
	$taobaocat_obj = Factory::getData('taobaocatcdian');
	$taobaoitemprop_obj = Factory::getData('taobaoitempropcdian');
	$taobaoitempropvalue_obj = Factory::getData('taobaoitempropvaluecdian');
}

$workdir = $argv[1];

// 获取类目的文件夹列表
$dirlist = getFileList($workdir);
if($dirlist === false){
	usage($argv, '获取类目的文件夹列表失败');
}

foreach($dirlist as $dir_name)
{
	$cur_dirname = $workdir . '/' . $dir_name;

	// 打开类目文件
	$category_file = $cur_dirname . '/categoryCsv';
	$fp = fopen($category_file, 'r');
	if($fp === false){
		usage($argv, "打开类目文件 {$category_file} 失败");
	}

	$first = true;
// 	$data = fgetcsv($fp, 1000, ',', '"', '\\');		// 此函数有bug，不能正确解析data中带[\]的段
	while($data = fgetcsv($fp, 1000, ',', '"', '\\'))
	{
		if($first){
			$first = false;
			continue;
		}

		$cid		= intval($data[0]);
		$parent_cid	= $data[1];
		$name		= $data[2];
		if($data[3] == 'true'){
			$is_parent	= 0;
		} else {
			$is_parent 	= 1;
		}

		$sort_order	= intval($data[4]);
		$features	= $data[5];

		$condition = array(
			'cid'	=> $cid,
			'name'	=> $name,
			'parent_cid'	=> $parent_cid,
			'is_parent'		=> $is_parent,
			'features'		=> $features,
			'modified_type' => '',
			'modified_time' => 0,
			'status'		=> 'normal',
			'sort_order'	=> $sort_order,
			'authorized'	=> 0,
		);

		if($taobaocat_obj->insert($condition) === false){
			printf("insert itemcat failed.sql = %s\n", $taobaocat_obj->getLastSql());
			exit(1);
		}


		if(count($data) != 6){
			printf("%s\n", $category_file);
			var_dump($data);
			exit(1);
		}
	}

	fclose($fp);


	// 打开类目属性文件
	$categoryprop_file = $cur_dirname . '/categoryPropCsv';
	$fp = fopen($categoryprop_file, 'r');
	if($fp === false){
		usage($argv, "打开类目属性文件 {$categoryprop_file} 失败");
	}

	$first = true;
	while($data = fgetcsv($fp, 1000, ',', '"', '\\'))
	{
		if($first){
			$first = false;
			continue;
		}

		if(count($data) != 13){
			printf("%s\n", $categoryprop_file);
			var_dump($data);
			exit(1);
		}

		$cid	= intval($data[0]);
		$pid	= intval($data[1]);
		$name	= $data[2];
		if($data[3] == 'true'){
			$must	= $required = 1;
		} else {
			$must	= $required = 0;
		}

		if($data[4] == 'true'){
			$is_key_prop = 1;
		} else {
			$is_key_prop = 0;
		}

		if($data[5] == 'true'){
			$is_sale_prop = 1;
		} else {
			$is_sale_prop = 0;
		}

		if($data[6] == 'true'){
			$is_color_prop = 1;
		} else {
			$is_color_prop = 0;
		}

		$child_template	= $data[7];
		$parent_pid		= intval($data[8]);
		$parent_vid		= intval($data[9]);
		$sort_order		= intval($data[10]);

		if($data[11] == 'true'){
			$is_allow_alias = 1;
		} else {
			$is_allow_alias = 0;
		}

		$type = $data[12];

		if(in_array($type, array('multiCheckText', 'multiCheck', 'optionalText', 'optional'))){
			$is_enum_prop = true;
		} else {
			$is_enum_prop = false;
		}

		$condition = array(
			'is_input_prop'		=> 0,
			'type'				=> $type,
			'modified_time'		=> 0,
			'modified_type'		=> '',
			'cid'				=> $cid,
			'required'			=> $required,
			'pid'				=> $pid,
			'parent_pid'		=> $parent_pid,
			'parent_vid'		=> $parent_vid,
			'name'				=> $name,
			'is_key_prop'		=> $is_key_prop,
			'is_sale_prop'		=> $is_sale_prop,
			'is_color_prop'		=> $is_color_prop,
			'is_enum_prop'		=> $is_enum_prop,
			'is_item_prop'		=> 0,
			'must'				=> $must,
			'multi'				=> 0,
			'prop_values'		=> '',
			'status'			=> 'normal',
			'sort_order'		=> $sort_order,
			'child_template'	=> $child_template,
			'is_allow_alias'	=> $is_allow_alias,
		);

		if($taobaoitemprop_obj->insert($condition) === false){
			printf("insert itemprop failed.sql = %s\n", $taobaoitemprop_obj->getLastSql());
			exit(1);
		}
	}


	fclose($fp);



	// 打开类目属性值文件
	$categorypropvalue_file = $cur_dirname . '/categoryPropValueCsv';
	$fp = fopen($categorypropvalue_file, 'r');
	if($fp === false){
		usage($argv, "打开类目属性值文件 {$categorypropvalue_file} 失败");
	}
	$first = true;
	while($data = fgetcsv($fp, 1000, ',', '"', '\\'))
	{
		if($first){
			$first = false;
			continue;
		}

		if(count($data) != 8){
			printf("%s\n", $categorypropvalue_file);
			var_dump($data);
			exit(1);
		}

		$cid	= intval($data[0]);
		$pid	= intval($data[1]);
		$vid	= intval($data[2]);
		$prop_name	= $data[3];
		$name		= $data[4];
		$name_alias	= $data[5];
		$sort_order	= intval($data[6]);
		if(empty($data[7])){
			$is_parent = 0;
		} else {
			$is_parent = 1;
		}

		$condition = array(
			'modified_time'		=> 0,
			'modified_type'		=> '',
			'cid'				=> $cid,
			'pid'				=> $pid,
			'prop_name'			=> $prop_name,
			'vid'				=> $vid,
			'name'				=> $name,
			'name_alias'		=> $name_alias,
			'is_parent'			=> $is_parent,
			'status'			=> 'normal',
			'sort_order'		=> $sort_order,
			'authorized'		=> 0,
		);

		if($taobaoitempropvalue_obj->insert($condition) === false){
			printf("insert itempropvalue failed.sql = %s\n", $taobaoitempropvalue_obj->getLastSql());
			exit(1);
		}
	}


	fclose($fp);
}



function getFileList($path)
{
	$handle = opendir($path);
	if($handle === false){
		return false;
	}

	$filelist = array();
	while($filename = readdir($handle))
	{
		if($filename == '.' || $filename == '..'){
			continue;
		}

		$filelist[] = $filename;
	}

	@closedir($handle);
	return $filelist;
}

